<?php

namespace App\Http\Controllers;

use Exception;
use Carbon\Carbon;
use App\Models\User;
use App\Models\Company;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Repositories\UserRepository;
use App\Repositories\CompanyRepository;
use App\Http\Requests\Logic\Company\SyncModules;
use App\Http\Requests\Logic\Company\StoreResource;
use App\Http\Requests\Logic\Company\QueryResource;
use App\Http\Requests\Logic\Company\UpdateResource;
use App\Http\Requests\Logic\Company\VettingRequest;
use App\Http\Requests\Logic\Company\DestroyResource;
use App\Http\Requests\Logic\Company\QueryCollection;
use App\Http\Requests\Logic\Company\BatchVettingRequest;

/**
 * 公司管理控制器
 *
 * Date: 22/03/2018
 * @author George
 * @package App\Http\Controllers
 */
class CompanyController extends Controller
{
	protected $userRepository;
	protected $companyRepository;

	/**
	 * CompanyController constructor.
	 * @param UserRepository $userRepository
	 * @param CompanyRepository $companyRepository
	 */
	public function __construct(
		UserRepository $userRepository,
		CompanyRepository $companyRepository
	)
	{
		$this->companyRepository = $companyRepository;
		$this->userRepository = $userRepository;
	}

	/**
	 * 获取公司信息集合
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param QueryCollection $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function index(QueryCollection $request)
	{
		$status = $request->get('status');
		$paginate = $request->get('paginate', 15);
		if ($status) {
			$this->companyRepository->where('status', '=', $status);
		}

		return success($this->companyRepository->paginate($paginate));
	}

	/**
	 * 获取根据主键公司信息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param QueryResource $request
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function show(QueryResource $request, Company $company)
	{
		return success($company);
	}

	/**
	 * 创建公司
	 *
	 * Date: 24/04/2018
	 * @author George
	 * @param StoreResource $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \App\Exceptions\GeneralException
	 * @throws \Throwable
	 */
	public function store(StoreResource $request)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());
		/**
		 * @var Company $company
		 */
		$company = $this->companyRepository->create($attributes);
		if ($company) {
			/**
			 * @var User $user
			 */
			$user = $request->user();
			$user->company_id = $company->id;
			$user->saveOrFail();
		}
		return stored($company, '创建成功，请等待审核');
	}

	/**
	 * 更新公司信息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param UpdateResource $request
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function update(UpdateResource $request, Company $company)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());
		try {
			$company->update($attributes);
			return updated($company);
		} catch (Exception $exception) {
			return internalError();
		}
	}

	/**
	 * 处理审核
	 *
	 * Date: 24/04/2018
	 * @author George
	 * @param VettingRequest $request
	 * @param $company
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
	public function vetting(VettingRequest $request, $company)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());

		if ($attributes['status'] == 'rejected') {
			DB::transaction(function () use ($attributes, $company) {
				DB::table('companies')->where('id', $company)->update(['status' => $attributes['status']]);
				DB::table('audits')->insert([
					'auditable_id' => $company,
					'auditable_type' => 'company',
					'proposer' => Auth::id(),
					'company_id' => $company,
					'status' => $attributes['status'],
					'comment' => $attributes['comment'],
					'created_at' => Carbon::now(),
					'updated_at' => Carbon::now()
				]);
			});
		} else {
			DB::table('companies')->where('id', $company)->update(['status' => $attributes['status']]);
		}

		$company = $this->companyRepository->getByPrimary($company);
		$company->audit = $company->audit()->get();
		return updated($company);
	}

	/**
	 * 批量通过
	 *
	 * Date: 24/04/2018
	 * @author George
	 * @param BatchVettingRequest $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
	public function batchVetting(BatchVettingRequest $request)
	{
		$ids = $request->get('ids');
		DB::table('companies')->whereIn('id', $ids)->update(['status' => 'verified']);
		return updated($ids);
	}

	/**
	 * 删除公司信息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param DestroyResource $request
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \App\Exceptions\GeneralException
	 */
	public function destroy(DestroyResource $request, Company $company)
	{
		$this->companyRepository->destroy($company);
		return deleted();
	}

	/**
	 * 获取公司模块信息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function modules(Company $company)
	{
		$modules = $this->companyRepository->getModules($company);

		return success($modules);
	}

	/**
	 * 同步公司模块绑定关系
	 *
	 * Date: 25/03/2018
	 * @author George
	 * @param SyncModules $request
	 * @param Company $company
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function syncModules(SyncModules $request, Company $company)
	{
		$modules = $request->get('modules');
		try {
			$company->modules()->sync($modules);
			return success($modules);
		} catch (Exception $exception) {
			return internalError();
		}
	}
}
